<div class="container">
  <h1>insert()</h1>
  <p class="signature">public function insert(array $data, ?string $target_table = null): ?int</p>
  <h2>Description</h2>
  <div class="description">
    <p>
      Insert a new record into the database table and return the ID of the newly inserted record. This method takes an associative array containing column names as keys and their corresponding values, along with an optional parameter specifying the name of the database table to insert into. It constructs and executes an SQL query to perform the insertion, returning the ID of the newly inserted record or null if insertion fails.
    </p>
  </div>
  <h2>Parameters</h2>
  <table>
    <thead>
      <tr>
        <th>Parameter</th>
        <th>Type</th>
        <th>Description</th>
        <th>Default</th>
        <th>Required</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>data</td>
        <td>array</td>
        <td>An associative array containing column names as keys and their corresponding values.</td>
        <td>N/A</td>
        <td>Yes</td>
      </tr>
      <tr>
        <td>target_table</td>
        <td>string|null</td>
        <td>(optional) The name of the database table to insert into. When a table name is not explicitly passed into the method, the table name will be assumed to be the value of the first URL segment.</td>
        <td>'First URL segment'</td>
        <td>No</td>
      </tr>
    </tbody>
  </table>
  <h2>Return Value</h2>
  <table>
    <thead>
      <tr>
        <th>Type</th>
        <th>Description</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>int|null</td>
        <td>The ID (int) of the newly inserted record, or null if insertion fails.</td>
      </tr>
    </tbody>
  </table>

  <h2>Example Usage #1</h2>
  <p>The following code demonstrates how to insert a new record into the 'users' table.</p>
  <div class="example">
    <pre>
$user_data = [
    'username' => 'john_doe',
    'email' => 'john@example.com',
    'password' => 'hashed_password'
];
$new_user_id = $this->model->insert($user_data, 'users');</pre>
  </div>

  <h2>Example Usage #2</h2>
  <p>In this example, we insert a new product record into an unspecified database table. Since no second argument has been passed into the method, the table name will be inferred from the first URL segment.</p>
  <div class="example">
    <pre>
$product_data = [
    'name' => 'Product X',
    'description' => 'A fantastic product',
    'price' => 99.99
];
$new_product_id = $this->model->insert($product_data);</pre>
  </div>
</div>
